package core.todo.dao;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.Extent;
import javax.jdo.PersistenceManager;


import core.todo.PMF;
import core.todo.domain.Todo;

public class TodoDAO {
	
	public TodoDAO() {
		super();
	}
	
	public void create(Todo newInstance) {
		PersistenceManager pm  = PMF.get().getPersistenceManager();
		pm.makePersistent(newInstance);
	}
	
	public List<Todo> findAll() {
		PersistenceManager pm  = PMF.get().getPersistenceManager();
		Extent<Todo> extent = pm.getExtent(Todo.class);
		ArrayList<Todo> todos = new ArrayList();
		for(Todo todo: extent) {
			todos.add(todo);
		}
		
		return todos;
	}
	
	public void update(Todo detatchedInstance) {
		PersistenceManager pm  = PMF.get().getPersistenceManager();
		pm.makePersistent(detatchedInstance);
	}
	
	public void delete(Long id) {
		PersistenceManager pm  = PMF.get().getPersistenceManager();
		Todo pTodo = pm.getObjectById(Todo.class, id);
		pm.deletePersistent(pTodo);
	}

	public Todo findById(Long id) {
		PersistenceManager pm  = PMF.get().getPersistenceManager();
		Todo pTodo = pm.getObjectById(Todo.class, id);
		return pTodo;
	}
}
